<?php 
defined('ACC')||exit('ACC Denied');
class Categorymodel extends Model{
	protected $table='category';
	protected $primarykey='cat_id';
	/**
	 * @param $data
	 * @param int $id
	 * @param int $lev
	 * @return array
	 */
	public function getCatTree($data,$id=0,$lev=0){
		static $tree=array();
		foreach($data as $k => $v) {
			if($v['parent_id']==$id) {
				$v['lev']=$lev;
				$tree[]=$v;
				$this->getCatTree($data,$v['cat_id'],$lev+1);
			}
		}
		return $tree;
	}
	/**
	 * @param $id
	 * @return array
	 * 	防止有下级栏目的栏目被删除
	 */	
	public function getSon($id){
		$sql="select * from ".$this->table.' where parent_id='.$id;
		return $this->db->getAll($sql);
	}
	public function getFamily($id=0){
		$tree=array();
		$arr=$this->select();
		while($id > 0){
		foreach ($arr as  $value) {
			if ($value['cat_id']==$id) {
				$tree[]=$value;
				$id=$value['parent_id'];
				break;
			}
			}
		}
		return array_reverse($tree);
	}
}